package com.wms.auto;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.wms.data.pojo.Role;

public class CreateDaoImpl {

	private static String PROP_FILE_PATH = "com.wms.data.pojo";
	private static String PROP_DEMP_FILE_NAME = "DemoDaoImpl.jtxt";
	
	private static String PROP_FILE_EN = "UTF-8";
	
	public static void main(String[] args) {
		
		try {
			
			CreateDaoImpl cp = new CreateDaoImpl();
			
			List<Map<String, String>> lstColumn = new ArrayList<Map<String,String>>();
			
			Map<String, String> map = new HashMap<String, String>();
			
			map.put(CreateFile.PROP_COLUMN_NAME, "user_name");
			map.put(CreateFile.PROP_COLUMN_NAME_LABEL, "用户名");
			map.put(CreateFile.PROP_COLUMN_TYPE, "VARCHAR");
			
			lstColumn.add(map);
			
			map = new HashMap<String, String>();
			
			map.put(CreateFile.PROP_COLUMN_NAME, "password");
			map.put(CreateFile.PROP_COLUMN_NAME_LABEL, "密码");
			map.put(CreateFile.PROP_COLUMN_TYPE, "VARCHAR");
			
			lstColumn.add(map);
			
			cp.create("sys_user", lstColumn);
			
			System.out.println(Role.class.getResource("").getPath() + "Role.java");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void create(String tableName, List<Map<String, String>> lstColumn) throws IOException {
		
		// 数据处理 
		String strName = CreateUtils.getNameToAaA(tableName);
		String inFilePath = CreateFile.class.getResource("").getPath() + PROP_DEMP_FILE_NAME;
//		String outFilePath = Role.class.getResource("").getPath() + strPojoName + ".java";
		String outFilePath = "E:/workspace/WMS/WMS/src/com/wms/data/dao/impl/" + strName + "DaoImpl" + ".java";
		
		File outF = new File(outFilePath);
		
		if (outF.exists()) {
			
			System.out.println("This is file existsed....table name: " + tableName + "");
			
			return;
		}
		
		// 以源文件编码格式读取源文件
		BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(inFilePath), PROP_FILE_EN));
		
		// 以目标文件编码格式输出目标文件
		PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFilePath), PROP_FILE_EN)));
		
		String reading;
		
		int lineIndex = 0;
		
		while ((reading = in.readLine()) != null) {
			
			lineIndex++;
			
			if (lineIndex == 29) {
				
				reading = StringUtils.replace(reading, "demo", CreateUtils.getNameToaaA(tableName));
				
				out.println(reading);
				
				continue;
			}
			
			if (lineIndex == 36) {
				
				int i = 0;
				
				reading = "\t\t\t\t";
				
				for (Map<String, String> map : lstColumn) {
					
					i++;
					
					reading += "\"" + map.get(CreateFile.PROP_COLUMN_NAME) + "\", ";
					
					if (i % 4 == 0) {
						
						out.println(reading);
						reading = "\t\t\t\t";
					}
					
				}
				
				if (i % 4 != 4) {
					
					out.println(reading);

					reading = "";
					
				}
				
				continue;
			}
			
			reading = StringUtils.replace(reading, "Demo", strName);
			
			out.println(reading);
		}
		
		out.close();
		in.close();
	
	}
}